package gclib.inventory;

import gclib.WeaponGroup;
import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.persistence.Column;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.Transient;

/**
 *
 * @author Jason
 */
@Entity
@DiscriminatorValue("Weapon")
public class Weapon extends Item implements Serializable{

    @Column(name="WEAPON_TYPE")
    @Enumerated(EnumType.STRING)
    protected CATEGORY weaponType = CATEGORY.IMPROVISED;
    @Column(name="COMBAT_TYPE")
    @Enumerated(EnumType.STRING)
    protected TYPE combatType = TYPE.MELEE;
    @Column(name="WEAPON_GROUP")
    @Enumerated(EnumType.STRING)
    protected GROUP group = GROUP.UNARMED;
    @Column(name="HANDED")
    @Enumerated(EnumType.STRING)
    protected HANDED handed = HANDED.ONE;
    @Column(name="PROFICIENCY_BONUS")
    protected Integer proficiencyBonus = 0;
    @Column(name="DIE_COUNT")
    protected Integer dieCount = 0;
    @Column(name="DIE_SIZE")
    protected Integer dieSize = 0;
    @Column(name="DAMAGE_TYPE")
    protected String dmgType = "";
    @Column(name="ENHANCEMENT")
    protected Integer enhancement = 0;
    @Column(name="ENHANCEMENT_TYPE")
    protected String enhancementDmgType = "";
    @Column(name="RANGE")
    protected String range;
    @Transient
    protected Set<PROPERTY> properties = new HashSet<PROPERTY>();
    @Transient
    private WeaponGroup weaponGroup;

    public Weapon() {
    }

    public WeaponGroup getWeaponGroup() {
        return weaponGroup;
    }

    public String getDamage(){
        return dieCount + "d" + dieSize + ((enhancement!=0)?enhancement:"") + enhancementDmgType;
    }

    public void parseDamage(String damageString) {
        //example: 2d6 + 1
        Pattern p;
        Matcher m;
        p = Pattern.compile("\\A\\d+(?=[Dd]\\d+)");
        m = p.matcher(damageString);
        String dieCountS;
        if (m.find()) {
            dieCountS = m.group();
        } else {
            dieCountS = "0";
        }

        p = Pattern.compile("(?<=\\A(\\d)+[Dd])(\\d)+");
        m = p.matcher(damageString);
        String dieSizeS;
        if (m.find()) {
            dieSizeS = m.group();
        } else {
            dieSizeS = "0";
        }
        this.dieCount = Integer.parseInt(dieCountS);
        this.dieSize = Integer.parseInt(dieSizeS);
    }

    public int getDieCount() {
        return dieCount;
    }

    public int getDieSize() {
        return dieSize;
    }

    public int getEnhancement() {
        return enhancement;
    }

    public String getEnhancementDmgType() {
        return enhancementDmgType;
    }

    public String getDamageType() {
        return dmgType;
    }

    public Set<PROPERTY> getProperties() {
        return properties;
    }

    public HANDED getHanded() {
        return handed;
    }

    public void setHanded(HANDED handed) {
        this.handed = handed;
    }

    public void setCombatType(TYPE combatType) {
        this.combatType = combatType;
    }

    public void setDieCount(int dieCount) {
        this.dieCount = dieCount;
    }

    public void setDieSize(int dieSize) {
        this.dieSize = dieSize;
    }

    public void setDmgType(String dmgType) {
        this.dmgType = dmgType;
    }

    public void setEnhancement(int enhancement) {
        this.enhancement = enhancement;
    }

    public void setEnhancementDmgType(String enhancementDmgType) {
        this.enhancementDmgType = enhancementDmgType;
    }

    public void setGroup(GROUP group) {
        this.group = group;
    }

    public void setProficiencyBonus(int proficiencyBonus) {
        this.proficiencyBonus = proficiencyBonus;
    }

    public void setProperties(Set<PROPERTY> properties) {
        this.properties = properties;
    }

    public void setRange(String range) {
        this.range = range;
    }

    public void setWeaponGroup(WeaponGroup weaponGroup) {
        this.weaponGroup = weaponGroup;
    }

    public void setWeaponType(CATEGORY weaponType) {
        this.weaponType = weaponType;
    }
    
    public static enum HANDED {

        ONE, TWO
    };

    public static enum GROUP {

        AXE, BOW, CROSSBOW, FLAIL, HAMMER, HEAVY_BLADE, LIGHT_BLADE, MACE, PICK, POLEARM, SLING, SPEAR, STAFF, UNARMED
    };

    public static enum PROPERTY {

        HEAVY_THROWN, HIGH_CRIT, LIGHT_THROWN, LOAD, OFF_HAND, REACH, SMALL, VERSATILE
    };

    public static enum CATEGORY {

        IMPROVISED, SIMPLE, MILITARY, SUPERIOR
    };

    public static enum TYPE {

        MELEE, RANGED
    };

}
